<div class="container-xl">
  <!-- Page title -->
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title" id="recommend_title"></h2>
      </div>
    </div>
  </div>
</div>
<!-- 业务页面代码 -->
<div class="page-body">
  <div class="container-xl">
    <div class="row row-cards">
      {% for item in Items %}
      <div class="col-6 col-md-3 col-xl-2">
        <div class="card card-sm card-link-pop rounded-3">
          {% if item.vote > 0%}
          <div class="badge badge-pill bg-purple" style="position:absolute;top:10px;right:10px;">{{ item.vote }}</div>
          {% endif %}
          <a href='javascript:show_mediainfo_modal("{{ RecommendType }}", "{{ item.title }}", "{{ item.year }}", "{% if RecommendType in ["hm", "nm", "ht", "nt"] %}{% else %}DB:{% endif %}{{ item.id }}", "recommend?t={{ RecommendType }}")'>
            <img src="{{ item.image or '../static/img/no-image.png' }}" class="card-img-top" style="border-top-left-radius:8px !important; border-top-right-radius:8px !important; object-fit:contain;">
          </a>
          <div class="card-body">
            <div class="d-flex align-items-center">
              <div>
                <div><strong>{{ item.title }}</strong></div>
              </div>
              <div class="ms-auto">
                <a id="recommend_link_{{ item.id }}" href='{% if item.fav == 0 %} javascript:show_recommend_add_modal("{{ item.id }}","{{ RecommendType }}","{{ item.title }}","{{ item.year }}"){% elif item.fav == 1 %}javascript:show_recommend_del_modal("{{ item.id }}","{{ RecommendType }}","{{ item.title }}","{{ item.year }}"){% else %}javascript:void(0){% endif %}' class="text-muted" title="{% if item.fav in [0,1] %}加入/取消订阅{% else %}已下载{% endif %}">
                  <svg id="recommend_svg_{{ item.id }}" xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-heart {% if item.fav == 0 %}{% elif item.fav == 1 %}icon-filled text-red{% else %}icon-filled text-green{% endif %}" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                     <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                     <path d="M19.5 13.572l-7.5 7.428l-7.5 -7.428m0 0a5 5 0 1 1 7.5 -6.566a5 5 0 1 1 7.5 6.572"></path>
                  </svg>
                </a>
              </div>
            </div>
          </div>
        </div>
      </div>
      {% endfor %}
    </div>
    {% if PageRange%}
    <div class="d-flex mt-3">
      <ul class="pagination ms-auto">
        <li class="page-item {% if CurrentPage==1 %} disabled {% endif %}">
          <a class="page-link" href="javascript:go_pre_page('{{ RecommendType }}', {{ CurrentPage }})" tabindex="-1" aria-disabled="true">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><polyline points="15 6 9 12 15 18" /></svg>
          </a>
        </li>
        {% for page in PageRange %}
        <li class="page-item {% if page==CurrentPage %} active {% endif %}"><a class="page-link" href="javascript:navmenu('recommend?t={{ RecommendType }}&page={{ page }}')">{{ page }}</a></li>
        {% endfor %}
        <li class="page-item">
          <a class="page-link" href="javascript:go_next_page('{{ RecommendType }}', {{ CurrentPage }})">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><polyline points="9 6 15 12 9 18" /></svg>
          </a>
        </li>
      </ul>
    </div>
    {% endif %}
  </div>
</div>
<script type="text/javascript">
  // 标题
  var title_type = '{{ RecommendType }}';
  if(title_type == "hm"){
    $("#recommend_title").text("TMDB热门电影");
  }
  if(title_type == "ht"){
    $("#recommend_title").text("TMDB热门电视剧");
  }
  if(title_type == "nm"){
    $("#recommend_title").text("TMDB最新电影");
  }
  if(title_type == "nt"){
    $("#recommend_title").text("TMDB最新电视剧");
  }
  if(title_type == "dbom"){
    $("#recommend_title").text("正在热映");
  }
  if(title_type == "dbhm"){
    $("#recommend_title").text("豆瓣热门电影");
  }
  if(title_type == "dbht"){
    $("#recommend_title").text("豆瓣热门电视剧");
  }
  if(title_type == "dbdh"){
    $("#recommend_title").text("豆瓣热门动漫");
  }
  if(title_type == "dbnm"){
    $("#recommend_title").text("即将上映");
  }
  if(title_type == "dbzy"){
    $("#recommend_title").text("豆瓣热门综艺");
  }
  // 上一页
  function go_pre_page(type, page){
    navmenu("recommend?t=" + type + "&page=" + (page - 1));
  }
  // 下一页
  function go_next_page(type, page){
    navmenu("recommend?t=" + type + "&page=" + (page + 1));
  }

  // 添加订阅关键字
  function add_rss_key(type, name, year, id){
      var cmd = "add_rss_media";
      if(['hm','nm','ht','nt'].includes(type)){
        var doubanid = "";
        var tmdbid = id;
      }else{
        var doubanid = id;
        var tmdbid = "";
      }
      var data = { "doubanid": doubanid, "tmdbid": tmdbid, "name": name, "type": type, "year": year};
      show_wait_process();
      ajax_post(cmd, data, function(ret){
        hide_wait_process();
        if(ret.code==0){
          show_success_modal(name + " 添加RSS订阅成功！");
          $("#recommend_svg_" + id).removeClass();
          $("#recommend_svg_" + id).addClass("icon icon-filled text-red");
          $("#recommend_link_" + id).attr("href", "javascript:remove_rss_key('" + type + "','" + name + "','" + year + "','" + id + "')");
        }else{
          show_fail_modal(name + " 添加RSS订阅失败："+ ret.msg +"！");
        }
      });
  }

  // 删除订阅关键字
  function remove_rss_key(type, name, year, id){
      var cmd = "remove_rss_media";
      var data = { "name": name, "type": type, "year": year};
      ajax_post(cmd, data, function(ret){
        show_success_modal(name + " 已从RSS订阅中移除！");
        $("#recommend_svg_" + id).removeClass();
        $("#recommend_svg_" + id).addClass("icon icon-tabler icon-tabler-heart");
        $("#recommend_link_" + id).attr("href", "javascript:add_rss_key('" + type + "','" + name + "','" + year + "','" + id + "')");
      });
  }

  // 显示添加订阅
  function show_recommend_add_modal(id, type, name, year){
    show_ask_modal("是否确定订阅： " + name + "？", function(){
      hide_ask_modal();
      add_rss_key(type, name, year, id);
    });
  }

  // 显示删除订阅
  function show_recommend_del_modal(id, type, name, year){
    show_ask_modal("是否确定将 " + name + " 从RSS订阅中移除？", function(){
      hide_ask_modal();
      remove_rss_key(type, name, year, id);
    });
  }

</script>
